package com.wst.system.util;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.symmetric.AES;
import lombok.extern.slf4j.Slf4j;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/6/16 11:58
 * @since JDK 1.8
 */
@Slf4j
public class CryptoContextUtils {


    private static final String SECRET="0CoJUm6Qyw8W8jud";

    private static final String SECRET2="0102030405060708";

    private static  AES aes = new AES(Mode.CTS, Padding.PKCS5Padding, SECRET.getBytes(), SECRET2.getBytes());


    /**
     * 加密
     * @param content
     * @return
     */
    public static String encrpytHex(String content){
        return aes.encryptHex(content,CharsetUtil.CHARSET_UTF_8);
    }

    /**
     * 解密
     * @param encrptContent
     * @return
     */
    public static String decryptStr(String encrptContent){
        try {
            return aes.decryptStr(encrptContent, CharsetUtil.CHARSET_UTF_8);
        }catch (Exception e){
            e.printStackTrace();
        }
        return encrptContent;
    }



    public static String decryptAES(String encryptedText, String key)  {
        try {
            // 密钥需要是16字节
            byte[] keyBytes = key.getBytes("UTF-8");
            // 创建密钥对象
            SecretKeySpec secretKey = new SecretKeySpec(keyBytes, "AES");
            // 创建Cipher对象，使用AES算法，模式为ECB，填充方式为PKCS7
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            // Base64解码
            byte[] encryptedBytes = Base64.getDecoder().decode(encryptedText);
            // 解密
            byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
            return new String(decryptedBytes, "UTF-8");
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * AES 加密
     *
     * @param plainText 待加密的明文
     * @param key       密钥（16字节）
     * @return 加密后的Base64编码字符串
     */
    public static String encryptAES(String plainText, String key) {
        try {
            // 密钥需要是16字节
            byte[] keyBytes = key.getBytes("UTF-8");
            // 创建密钥对象
            SecretKeySpec secretKey = new SecretKeySpec(keyBytes, "AES");
            // 创建Cipher对象，使用AES算法，模式为ECB，填充方式为PKCS5Padding
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            // 加密
            byte[] encryptedBytes = cipher.doFinal(plainText.getBytes("UTF-8"));
            // Base64编码
            return Base64.getEncoder().encodeToString(encryptedBytes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将16进制转换为二进制
     *
     * @param hexStr
     * @return
     */
    private static byte[] parseHexStr2Byte(String hexStr) {
        if (hexStr.length() < 1) {
            return null;
        }
        log.info(hexStr);
        byte[] result = new byte[hexStr.length() / 2];
        for (int i = 0; i < hexStr.length() / 2; i++) {
            int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
            int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
            result[i] = (byte) (high * 16 + low);
        }
        return result;
    }


    public static void main(String[] args) {
        try {
            String s=decryptAES("pVHk1Uz8OWzJPIndVfZYfA==","1538663015386630");
            System.out.println(s);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



}
